<%@ page import="cn.js.fan.util.ParamUtil" %>
<%@ page import="com.redmoon.oa.person.UserDesktopSetupDb" %>
<%@ page import="com.redmoon.oa.flow.FormDb" %>
<%@ page import="com.redmoon.oa.visual.FormDAO" %>
<%@ page import="com.redmoon.oa.visual.ModuleSetupDb" %>
<%@ page import="com.redmoon.oa.sys.DebugUtil" %>
<%@ page import="com.redmoon.oa.flow.FormField" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.redmoon.oa.visual.ModuleUtil" %>
<%@ page import="cn.js.fan.util.ErrMsgException" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="cn.js.fan.util.StrUtil" %>
<%@ page import="com.redmoon.oa.chart.ChartUtil" %>
<%@ page contentType="text/html;charset=utf-8" %>
<%
    int id = ParamUtil.getInt(request, "id", -1);
    UserDesktopSetupDb udsd = new UserDesktopSetupDb();
    udsd = udsd.getUserDesktopSetupDb(id);

    long chartId = StrUtil.toLong(udsd.getMetaData(), -1);
    if (chartId == -1) {
        out.print("未设置关联的图形");
        DebugUtil.e(getClass(), "桌面：", udsd.getTitle() + " 未设置关联的图形！");
        return;
    }

    FormDb fdChart = new FormDb();
    fdChart = fdChart.getFormDb(ChartUtil.CONFIG_CHART);
    FormDAO fdaoChart = new FormDAO();
    fdaoChart = fdaoChart.getFormDAO(chartId, fdChart);
    String title = fdaoChart.getFieldValue("title");
    String fieldName = fdaoChart.getFieldValue("field_name");
    String moduleCode = fdaoChart.getFieldValue("module_code");
    boolean isAaverage = "1".equals(fdaoChart.getFieldValue("is_average"));

    ModuleSetupDb msd = new ModuleSetupDb();
    msd = msd.getModuleSetupDb(moduleCode);
    if (msd == null) {
        DebugUtil.e(getClass(), "getModuleSetupDb", "模块：" + moduleCode + "不存在！");
        out.print("模块：" + moduleCode + "不存在！");
        return;
    }
    String formCode = msd.getString("form_code");

    FormDb fd = new FormDb();
    fd = fd.getFormDb(formCode);
    FormField ff = fd.getFormField(fieldName);

    JSONArray arr = null;
    try {
        arr = ChartUtil.getDataForChart(request, msd, fd, ff);
    } catch (ErrMsgException e) {
        out.print(e.getMessage());
        return;
    }
%>
<script>
    // 实例化一个JS数组
    var xItems = new Array();
    var yItems = new Array();
    // 循环遍历Java数组
    <%
        for (int i = 0; i < arr.size(); i++) {
            JSONObject json = arr.getJSONObject(i);
    %>
            // 将Java数组中的值赋给JS数组
    xItems[<%=i%>] = '<%=json.getString("name")%>';
    yItems[<%=i%>] = '<%=json.getString("value")%>';
    <%
         }
    %>
</script>
<div id="drag_<%=id%>" class="portlet drag_div bor ibox" style="padding:0;">
    <div id="drag_<%=id%>_h" class="drag-h" style="height:3px;padding:0;margin:0;font-size:1px"></div>
    <div class="ibox-title">
        <div class="chart-title">
            <h5>
                <i class="fa <%=udsd.getIcon()%>"></i>
                <span><%=udsd.getTitle()%></span>
            </h5>
        </div>
    </div>
    <div class="portlet_content ibox-content">
        <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
        <div id="barBox<%=id%>" style="width: 350px;height:210px;"></div>
        <script type="text/javascript">
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById("barBox<%=id%>"));
            // 指定图表的配置项和数据
            var option = {
                grid:{
                    y: 25,
                    height:160
                },
                // 标题
                /*title: {
                    text: '示例',
                    subtext:'出品',
                    left:'left',
                    borderColor:'red',
                    borderWidth:3
                },*/
                // 工具箱
                toolbox: {
                    show: false,
                    feature:{
                        saveAsImage:{
                            show:true
                        },
                        restore:{
                            show:true
                        },
                        dataView:{
                            show:true
                        },
                        dataZoom:{
                            show:true
                        },
                        magicType:{
                            type:['line','bar']
                        }
                    }
                },
                tooltip:{
                    trigger:'axis'
                },
                // 图例
                /*legend: {
                    data: ['title']
                },*/
                // x轴
                xAxis: {
                    data: xItems
                },
                yAxis: {},
                // 数据
                series: [{
                    name: '<%=ff.getTitle()%>',
                    type: 'bar',
                    data: yItems,
                    itemStyle: {
                        color: new echarts.graphic.LinearGradient(
                            0, 0, 0, 1,
                            [
                                {offset: 0, color: '#83bff6'},
                                {offset: 0.5, color: '#188df0'},
                                {offset: 1, color: '#188df0'}
                            ]
                        )
                    },
                    emphasis: {
                        itemStyle: {
                            color: new echarts.graphic.LinearGradient(
                                0, 0, 0, 1,
                                [
                                    {offset: 0, color: '#2378f7'},
                                    {offset: 0.7, color: '#2378f7'},
                                    {offset: 1, color: '#83bff6'}
                                ]
                            )
                        }
                    },
                    /*markPoint:{
                        data:[
                            {type:'max',name:'最大值'},
                            {type:'min',name:'最小值',symbol:'arrow'}
                        ]
                    },*/
                    markLine:{
                        <%
                        if (isAaverage) {
                        %>
                        data:[
                            {type: 'average', name:'平均值'}
                        ]
                        <%
                        }
                        %>
                    }
                }
                ]
            };

            myChart.setOption(option);
        </script>
    </div>
</div>
